local zishucup__qianshang = fk.CreateSkill{
  name = "zishucup__qianshang",
}

Fk:loadTranslationTable{
  ["zishucup__qianshang"] = "千觞",
  [":zishucup__qianshang"] = "出牌阶段，你可以将你，上家，下家各一张牌当【酒】对其中一名角色使用，然后本回合下次有角色造成伤害后，此牌目标获得弃牌堆中的底牌。",

  ["#zishucup__qianshang"] = "千觞：请选择你要用于转化的牌与以此法使用酒的角色，上下家的牌随后选择",
  ["#zishucup__qianshang-two"] = "千觞: 上下家一致，请选择其的两张牌用于转化",

  ["$zishucup__qianshang1"] = "军中设市，得四方美酿，邀诸君共饮。",
  ["$zishucup__qianshang2"] = "锦衣玉食到手，杀几个丘八算什么？",
}

zishucup__qianshang:addEffect("active", {
  anim_type = "offensive",
  prompt = "#zishucup__qianshang",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return player:canUse(Fk:cloneCard("analeptic")) and not player:isNude()
    and (not player:getNextAlive():isNude() or not player:getLastAlive():isNude())
    and (player:getNextAlive() ~= player or player:getLastAlive() ~= player)
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return player:canUseTo(Fk:cloneCard("analeptic"), to_select) and (to_select == player or
    to_select == player:getLastAlive() or to_select == player:getNextAlive()) and #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local lastone = player:getLastAlive() ---@class ServerPlayer
    local nextone = player:getNextAlive() ---@class ServerPlayer
    local analeptic = Fk:cloneCard("analeptic")
    analeptic.skillName = zishucup__qianshang.name
    local n = 0
    for i = 1, 50 ,1 do --50个应该够用
      if #target:getTableMark("zishucup__qianshang"..i.."-turn") == 0 then
        n = 0 + i
        break
      end
    end
    if lastone ~= nextone then
      local cards1 = room:askToChooseCards(player, { min = 1, max = 1 , target = lastone, flag = "he", skill_name = zishucup__qianshang.name })
      local cards2 = room:askToChooseCards(player, { min = 1, max = 1 , target = nextone, flag = "he", skill_name = zishucup__qianshang.name })
      for _, id in ipairs({effect.cards, cards1, cards2}) do
        room:addTableMark(target, "zishucup__qianshang"..n.."-turn", id[1])
        analeptic:addSubcard(id[1])
      end
    elseif lastone == nextone then
      local m = math.min(2, #lastone:getCardIds("he"))
      local cards1 = room:askToChooseCards(player, { min = m, max = m , target = lastone, flag = "he", 
        skill_name = zishucup__qianshang.name , prompt = "#zishucup__qianshang-two"})
      for _, id in ipairs({effect.cards, cards1}) do
        room:addTableMark(target, "zishucup__qianshang"..n.."-turn", id[1])
        analeptic:addSubcard(id[1])
      end
    end
    room:useCard{
      from = player,
      card = analeptic,
      tos = { target },
    }
  end,
})

zishucup__qianshang:addEffect(fk.Damage, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    for i = 1, 50 ,1 do --50个应该够用
      return #player:getTableMark("zishucup__qianshang"..i.."-turn") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    for i = 1, 50 ,1 do --50个应该够用
      if player.dead then break end
      if #player:getTableMark("zishucup__qianshang"..i.."-turn") > 0 then
        local toget = {}
        for _, t in ipairs(player:getTableMark("zishucup__qianshang"..i.."-turn")) do
          if table.contains(player.room.discard_pile, t) then
            table.insertIfNeed(toget, t)
          end
        end
        player.room:setPlayerMark(player, "zishucup__qianshang"..i.."-turn", 0)
        player.room:moveCardTo(toget, Card.PlayerHand, player, fk.ReasonPrey, zishucup__qianshang.name, nil, true, player)
      end
    end
  end,
})

return zishucup__qianshang
